home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / spwno413.zip / SPAWNO.DOC < prev    next >
Text File  |  1992-12-12  |  22KB  |  487 lines

  1. SPAWNO v4.13   12/12/91   disk/EMS/XMS/INT15 swapping replacement for spawn()
  2. (c) Copyright 1990,1991,1992 Ralf Brown.  All Rights Reserved.
  3.  
  4.  
  5. -----------------
  6. LICENSE
  7.  
  8. This document and the associated header file, libraries, and source and
  9. object modules may be freely copied provided that:
  10.      1) all of the files are copied as a group (such as in a single
  11.     archive).  See below for a list of files.
  12.      2) the files are not modified in any way (including removal or
  13.     alteration of copyright notices or this license)
  14.      3) no charge beyond a cost-recovery fee not to exceed $5, or a
  15.     general telecommunications connection fee, is made
  16.  
  17. The source and/or object code comprising the public SPAWNO distribution
  18. may be incorporated at no charge into programs which are distributed or
  19. sold to others provided that:
  20.      1) credit is given in the documentation.  A mention such as
  21.        "This product uses the SPAWNO routines by Ralf Brown
  22.         to minimize memory use while shelling to DOS and
  23.         running other programs."
  24.     is sufficient.
  25.      2) you drop me a line telling me that you are using SPAWNO and
  26.     in which product(s) you are using it (see addresses at the
  27.     end of this file).  I'm interested in finding out how wide-
  28.     spread the use of SPAWNO becomes.
  29.  
  30.  
  31. -----------------
  32. DISCLAIMER
  33.  
  34. Although both I and others have tested the code which comprises SPAWNO,
  35. it is entirely possible that SPAWNO may malfunction in environments or
  36. under circumstances in which it has not been tested.  This code is
  37. provided AS IS, and the author disclaims any and all responsibility for
  38. damages (both consequential and incidental) resulting from the use or
  39. misuse of SPAWNO.  Sole responsibility for determining the suitability
  40. of the code rests with the user.
  41.  
  42.  
  43. -----------------
  44. FILES
  45.  
  46. The disk or archive you received should contain the following files:
  47.  
  48.     SPAWNO.DOC    this file
  49.     SPAWNO.H    the header file providing prototypes for the functions
  50.     SPAWNx.LIB    versions of the library for each of [T]iny, [S]mall,
  51.             [C]ompact, [M]edium, [L]arge, and [H]uge memory
  52.             models.
  53.     SPAWNTP.OBJ    Turbo Pascal version of SPAWNO
  54.     SPAWNO.PAS    Turbo Pascal unit encapsulating SPAWNO
  55.     SPAWN_MS.ZIP    Microsoft C versions of compiler-specific functions
  56.     MSC.BAT        replace Turbo C specific functions in libraries with
  57.             Microsoft C versions.
  58.     NOREPLAC.BAT    remove replacements for the standard spawn..()
  59.             functions from the libraries.
  60.     SWAPTEST.C    example program for testing
  61.     TEST.PAS    example program for testing
  62.     SOURCE.ZIP    archive containing partial source code for SPAWNO
  63.         _SPAWNO.H        header file for recompiling library
  64.         *.C            source for the spawn..() and spawn...o() functions
  65.         RULES.ASI        shortened and modified Turbo C macro file
  66.         SPAWNO.INC        include file for various important constants
  67.         SPAWNPTH.ASM    source for the path search
  68.         CHECKEXT.ASM    check for 'file', 'file.EXE', and 'file.COM'
  69.         SWAP_EMS.ASM    support routines for swapping to EMS
  70.         SWAPLIST.ASM    specify which swap types will be linked in
  71.         SPAWNENV.ASM    make an environment block from an array of strings     
  72.         SPAWNERR.ASM    set _doserrno and errno
  73.         MAKEFILE
  74.  
  75.  
  76. -----------------
  77. WHAT'S NEW
  78.  
  79. Changes/fixes from versions 4.00 to 4.10:
  80.     Now swaps to disk with non-NULL swap list in large-data memory models
  81.     Now correctly deletes swap file if child program changes directories
  82.     If the given program name is "FOO", and both "FOO" and either
  83.        "FOO.COM" and/or "FOO.EXE" exist in the same directory, it will
  84.        now execute either "FOO.COM" or "FOO.EXE" rather than "FOO", as
  85.        both version 4.00 and the Borland library functions do.  To
  86.        execute "FOO", pass "FOO." rather than just "FOO" to the function.
  87.     Fixed bug which may have led to a partially corrupted environment
  88.        in a few circumstances
  89. Changes/fixes since version 4.10:
  90.     Fixed bug which caused the PSP to restore incorrectly after a spawn
  91.        when DS != SS.
  92.     Fixed bug introduced in v4.10 which caused a crash on return from the
  93.        child program if there was insufficient disk space for the swap
  94.        file.
  95.     Fixed error in va_start call in various *.C files.  This problem
  96.        results in compile-time errors when compiling under BC++ in
  97.        certain modes (but the code compiled without warnings under
  98.        Turbo C 2.0).
  99.  
  100. -----------------
  101. INSTALLATION
  102.  
  103. If you are using Turbo C, Turbo C++, or Borland C++, simply dearchive
  104. the libraries and header file and place them where your compiler can
  105. find them.  If you are using Microsoft C, dearchive SPAWN_MS.ZIP and
  106. then run the MSC batch file to replace the compiler-specific functions
  107. in the libraries with Microsoft C versions.  Please do not run this
  108. batch file on your original copy of the libraries.
  109.  
  110. If you are using Turbo Pascal, unarchive SPAWNTP.OBJ and SPAWNO.PAS.
  111. Compile SPAWNO.PAS and then add a USES SPAWNO; line to your program.
  112. After that you can call the function SPAWN() as desired.  The
  113. distribution does not contain a pre-compiled version because .TPU
  114. files are not compatible between different versions of Turbo Pascal.
  115.  
  116.  
  117. -----------------
  118. INSTRUCTIONS
  119.  
  120. As distributed, SPAWNO contains replacements for the standard spawn..()
  121. functions.  You can use SPAWNO with absolutely no changes to your
  122. existing source code; however, you will probably want to call
  123. init_SPAWNO() from main(), and include SPAWNO.H for that source file.
  124. The initialization function takes two arguments: the first is a string
  125. containing a list of directories (separated by semicolons) in which to
  126. attempt to store the swap file when swapping to disk. The second
  127. specifies which swap devices to attempt to use; it may be either
  128. SWAP_ANY or the ORing or addition of any combination of SWAP_DISK,
  129. SWAP_EMS, SWAP_EXT, and SWAP_XMS (such as SWAP_DISK|SWAP_XMS).    If you
  130. do not call init_SPAWNO(), the defaults are to attempt all swap devices
  131. and to store the swap file in the current directory when swapping to
  132. disk.  init_SPAWNO() will set the swap file directory list to the first
  133. of the following to have a non-NULL, non-empty value: the environment
  134. variable SWAPDIR, the passed parameter, the environment variable TEMP,
  135. and the environment variable TMP.  Note that init_SPAWNO is the only
  136. function in the library which uses these environment variables.
  137.  
  138. After making the above change, recompile your existing code, but include
  139. the SPAWNO library in the link step.  Note that SPAWNO does not support
  140. the P_OVERLAY spawn type--any calls using that argument should be
  141. replaced with the corresponding exec..() function.  If a SPAWNO function
  142. is called with P_OVERLAY, it simply returns an error.
  143.  
  144. For backwards compatibility with previous versions of SPAWNO, and for
  145. those who (for whatever reason) require the use of both the swapping and
  146. standard spawn..() functions, there is a second set of functions which
  147. take an explicit swap directory argument.  To use them, include the
  148. header file SPAWNO.H.  Then use the functions provided in this
  149. archive--spawnvo(), spawnlo(), spawnvpo(), etc--as you would the
  150. corresponding spawnv(), spawnl(), etc, except replace the first argument
  151. by a string containing the path where the swap file is to be stored.
  152. Note that the swap file path is required even when swapping to XMS, EMS,
  153. or plain extended memory, as SPAWNO will swap to disk anyway if there is
  154. not enough memory available.
  155.  
  156. If you require the use of both the swapping and standard spawn..()
  157. functions, you will need to run the NOREPLAC.BAT batch file to remove
  158. the modules containing the replacements for the standard functions or
  159. arrange the linker command line such that SPAWNx.LIB is listed after the
  160. standard runtime library (for Turbo/Borland C users, the latter requires
  161. a separate TLINK command, as TCC/BCC places any libraries specified on
  162. the command line prior to the runtime library).     Please do not run
  163. NOREPLAC on your original copy of the libraries, as the process is
  164. destructive and cannot be reversed.
  165.  
  166. The spawn?p??() functions search for both .COM and .EXE files in the
  167. current directory and then in the pathed directories if no extension is
  168. given, but use the specified extension if present.  If a full pathname
  169. is given to those functions, only the specified directory is searched.
  170. If both a path and an extension are given, only that specific file will
  171. be loaded.  The other spawn..() functions check the current directory
  172. only.  For all functions, if the specified filename does not contain an
  173. extension, SPAWNO will try the exact name given and the name with .COM
  174. and .EXE extensions. The extension-less name will be used if neither
  175. .COM nor .EXE exist.  To force the use of the extensionless file even
  176. if a .COM or .EXE with the same name exists in the same directory,
  177. append a period, i.e. use "PROGRAM." rather than just "PROGRAM" as
  178. the name.
  179.  
  180. When linking, you must include SPAWNx.LIB.  A sample TCC line would be
  181.  
  182.     tcc -mc <other-flags> sample spawnc.lib library.lib
  183.  
  184. This results in 208 to 288 bytes remaining in memory (depending on swap
  185. type) while the spawned program executes.  However, if you want to keep
  186. an interrupt hooked which might be invoked while spawned, you must set
  187. __spawn_resident large enough that *all* code and data which could be
  188. invoked by that interrupt is kept resident[1].    SPAWNO automatically
  189. deactivates INT 23h and INT 24h and restores them before returning to
  190. the caller unless __spawn_keepints is set to 1, so no extra work is
  191. required if those two are the only interrupts hooked by the program.
  192.  
  193. ----
  194. [1] Due to the segment layout used by C compilers, this option is probably
  195.     useful only when calling SPAWNO from assembly-language code (which can
  196.     localize such code and data near the start of the executable) or a
  197.     small program which uses a large amount of space in the far heap.
  198.  
  199.  
  200. -----------------
  201. ERROR CODES
  202.  
  203. SPAWNO will return errno = ENOMEM (_doserrno = 8) if there is insufficient
  204. swap space available.
  205.  
  206.  
  207. -----------------
  208. ERROR MESSAGES
  209.  
  210. SPAWNO: stack too small, retrying
  211.     SPAWNO did not correctly determine the stack requirements for
  212.     the system on which it is running, but was able to determine
  213.     that the specified minimum size is insufficient.  On the retry,
  214.     the stack size is increased by one paragraph.  Since SPAWNO
  215.     cannot always detect the problem, which would result in
  216.     memory chain corruption, you should increase the value of
  217.     __spawn_res_stack to prevent a recurrence of this error
  218.     message.  For most systems, the default value of 4 paragraphs
  219.     will be sufficient.
  220.  
  221. SWAP ERR
  222.     The resident stub detected an error while trying to reload the
  223.     main portion of the program and aborted.  This may be due to
  224.     loading a TSR while swapped out or deleting the swap file.
  225.  
  226. SPAWNO: error reloading program, aborting
  227.     The main portion of SPAWNO detected an error while attempting
  228.     to restore the remainder of the program to memory.
  229.  
  230.  
  231. -----------------
  232. LIMITATIONS
  233.  
  234. After freeing all memory owned by the program except the initial (PSP)
  235. memory block and the environment, at least 384 bytes of conventional
  236. memory must be available.  SPAWNO uses this for its temporary stack.
  237. This is of particular importance for the Turbo Pascal variant, because
  238. setting the maximum heap size to the available memory or greater will
  239. use up all RAM and prevent SPAWNO from allocating its temporary stack
  240. unless memory is fragmented or there is DOS 5 high memory available.
  241.  
  242. SPAWNO may report insufficient memory even though the program to be
  243. executed would fit with up to 600 bytes to spare, due to the fact that
  244. part of the stack and all of the data used by DOS to start the program
  245. may be overwritten after the child program starts executing.
  246.  
  247. Functions which pass an explicit environment attempt to allocate enough
  248. memory to build the environment block which is passed to the DOS EXEC
  249. function.  If neither malloc() nor a DOS memory allocation request are
  250. successful, SPAWNO passes the original environment rather than the
  251. specified environment.
  252.  
  253. As distributed, SPAWNO functions which do not take an explicit
  254. environment pointer pass the calling program's original environment to
  255. the child, rather than the current environment as modified by putenv().
  256. You may recompile SPAWNO to pass the current environment (see
  257. _SPAWNO.H); if you do so, the caveat discussed in the previous paragraph
  258. will apply to all of the spawn..() functions.
  259.  
  260.  
  261. -----------------
  262. SUPPORT
  263.  
  264. Since I am not getting any money, I can't promise any support.    Those
  265. who have purchased the complete source code will receive priority over
  266. those who haven't, but even they will only receive support to the
  267. extent that I have spare time....
  268.  
  269.  
  270. -----------------
  271. ACKNOWLEDGEMENTS
  272.  
  273. Thanks to Gene McManus for testing the Microsoft versions of SPAWNO 3.0
  274. and 4.0.
  275.  
  276.  
  277. -----------------
  278. FUTURE PLANS
  279.  
  280. The next release will drop support for DOS 2.x (which is now over
  281. eight years old) to reduce the code size and further reduce the size
  282. of the resident stub.
  283.  
  284.  
  285. -----------------
  286. SPAWNO versus THE COMPETITION
  287.  
  288. Product:        SPAWNO        XSPAWN        SWAP
  289. Version:         4.13         1.34         1.0
  290. Author:             Ralf Brown      Whitney Software  Marty Del Vecchio
  291. Memory models:        tsmclh        tsmclh         tsm
  292. Supported DOS vers:    2.0-5.0           2.1-5.0          3.0-5.0
  293. Code size [1]:           4.3-5.9K        4.4-9K         1.5K
  294. Resident stub (bytes): 208-288          1277+2*env     1.8K+env [2]
  295. Direct replacement for
  296.     spawn..():         yes         yes          no
  297. Use in existing code
  298.     without changes:     yes          no          no
  299. Swap to disk:         yes         yes         yes
  300. Swap to EMS:         yes         yes         yes
  301. Swap to XMS:         yes          no          no
  302. Swap to INT 15h ext:     yes          no          no
  303. Multiple swap dirs:     yes         yes          no
  304. Automatic unique swap
  305.     file naming:     yes         yes          no
  306. May leave interrupts
  307.     hooked [3]:         yes          no         yes
  308. Swaps DOS5 high memory:     yes           ?           ?
  309. Free for any use:     yes         yes         yes
  310. Source included:       partial [4]     yes         yes
  311.  
  312. Notes:
  313. [1] For both SPAWNO and XSPAWN, the amount added to the executable's
  314.     size depends on which functions of the library are called and which
  315.     of the additional C runtime library function that are called by
  316.     SPAWNO/XSPAWN would have been included anyway.  SPAWNO's size may
  317.     be reduced by removing one or more swap types; the savings are
  318.     about 400 bytes per swap type (without EMS and non-XMS (INT 15h)
  319.     swapping, SPAWNO would add as little as 3.4K to the executable).
  320.  
  321. [2] SWAP's resident size depends on the location of the object code
  322.     within the executable.  The size reported here is the resident
  323.     size when SWAP immediately follows the Turbo C 2.0 startup code.
  324.     It will be larger in later versions of the Borland C compilers due
  325.     to the increasing size of the startup code.
  326.  
  327. [3] For SPAWNO, enough of the program must be left in memory to keep all
  328.     interrupt handlers resident.  XSPAWN has no provision for keeping
  329.     an interrupt handler resident unless swapping is disabled; it does
  330.     however permit selection whether a given interrupt handler will
  331.     automatically be restored to its original value or pointed at an
  332.     IRET instruction while swapped out.     For SWAP, the swapper module
  333.     must be linked in after all code and data which might be accessed
  334.     by the interrupt handler(s).
  335.  
  336. [4] Full SPAWNO source code is available for licensing.  See ORDER.FRM for
  337.     details.
  338.  
  339.  
  340. -----------------
  341. Functions:
  342.     void init_SPAWNO(const char *swap_directories, int swap_types) ;
  343.  
  344.     /* replacements for the standard functions */
  345.     int spawnv(int type, const char *name, const char **args) ;
  346.     int spawnvp(int type, const char *name, const char **args) ;
  347.     int spawnve(int type, const char *name, const char **args,
  348.             const char **env) ;
  349.     int spawnvpe(int type, const char *name, const char **args,
  350.              const char **env) ;
  351.     int spawnl(int type, const char *name, const char *argv0, ...) ;
  352.     int spawnlp(int type, const char *name, const char *argv0, ...) ;
  353.     int spawnle(int type, const char *name, const char *argv0, ...) ;
  354.     int spawnlpe(int type, const char *name, const char *argv0, ...) ;
  355.     int system(const char *command) ;
  356.     /* Note: the above functions return -1 (error) if 'type' is not */
  357.     /*     P_WAIT     */
  358.  
  359.     /* setup function */
  360.     void init_SPAWNO(const char *overlay_path, int swap_types) ;
  361.     /* setup swapping options for the compatibility functions above */
  362.  
  363.     /* SPAWNO-specific functions */
  364.     int spawnvo(const char *overlay_path, const char *name,
  365.             const char **args) ;
  366.     int spawnvpo(const char *overlay_path, const char *name,
  367.              const char **args) ;
  368.     int spawnveo(const char *overlay_path, const char *name,
  369.              const char **args,const char **env) ;
  370.     int spawnvpeo(const char *overlay_path, const char *name,
  371.              const char **args,const char **env) ;
  372.     int spawnlo(const char *overlay_path, const char *name,
  373.              const char *argv0, ...) ;
  374.     int spawnlpo(const char *overlay_path, const char *name,
  375.              const char *argv0, ...) ;
  376.     int spawnleo(const char *overlay_path, const char *name,
  377.              const char *argv0, ...) ;
  378.     int spawnlpeo(const char *overlay_path, const char *name,
  379.              const char *argv0, ...) ;
  380.     int systemo(const char *overlay_path, const char *command) ;
  381.     /* Note: the ..o() functions use only the current directory if */
  382.     /*     overlay_path is NULL; the only function which checks  */
  383.     /*     the environment variables SWAPDIR and TEMP is           */
  384.     /*     init_SPAWNO().                        */
  385.  
  386. Global variables:
  387.        char __spawn_xms ;
  388.         Specify whether to use XMS memory for swapping (if available).
  389.         0 = no, 1 = yes, default = 1
  390.         This variable is set by init_SPAWNO().
  391.        char __spawn_ems ;
  392.         Specify whether to use EMS memory for swapping (if available).
  393.         0 = no, 1 = yes, default = 1
  394.         This variable is set by init_SPAWNO().
  395.        char __spawn_ext ;  /* 0 = don't use non-XMS extended memory for swap */
  396.         Specify whether to use non-XMS extended memory (via INT 15h) for
  397.         swapping (if available).  0 = no, 1 = yes, default = 1
  398.         This variable is set by init_SPAWNO().
  399.         Note: SPAWNO should not be allowed to swap to extended memory if
  400.         running under a multitasker or task switcher, as other programs
  401.         would be able to grab the same memory used by SPAWNO to store the
  402.         swapped-out program.  For this reason, SPAWNO automatically
  403.         disables swapping to non-XMS extended memory if it detects that
  404.         TopView, DESQview, or any other TopView API-compatible multi-
  405.         tasker is active, or the DOS 5 task switcher is loaded, or
  406.         MS Windows 3 is running in real or standard mode (enhanced mode
  407.         is not readily detectable from a program, unfortunately).
  408.        const char *pascal ___spawn_swap_dirs ;
  409.         Specify the list of directories in which to attempt to store
  410.         the swap file when swapping to disk.  The directories are
  411.         separated by semicolons just as they would be in the PATH
  412.         environment variable.  Default = NULL (same as ".": current
  413.         directory)
  414.         This variable is set by init_SPAWNO().
  415.         Note: SPAWNO will not swap to disk if this variable is set to an
  416.         empty string; if it is set to NULL, SPAWNO will use the default
  417.         directory list of ".".
  418.        char __spawn_keepints ;
  419.         Specify whether to deactivate INT 23h and INT 24h handlers by
  420.         temporarily restoring the vectors stored in the PSP.
  421.         0 = no, nonzero = yes, default = 0
  422.        char __spawn_swap_UMA ;
  423.         Specify whether to swap out memory blocks in the upper memory
  424.         area (640K-1M).  0 = no, 1 = yes, default = 1
  425.         Note: This option only has an effect under MSDOS 5.0 or higher
  426.         with the line DOS=UMB or DOS=HIGH,UMB in CONFIG.SYS.
  427.        unsigned __spawn_resident ;
  428.         Specify the minimum number of paragraphs in the memory block
  429.         containing the PSP to keep resident while in the child program.
  430.         This value will be increased if necessary to be large enough to
  431.         hold the swapping code; it may also be increased slightly if it
  432.         causes the end of the shrunken PSP block to fall within a
  433.         specific section of SPAWNO's code.  You may force the entire
  434.         main block to stay in memory by setting this variable to 0xFFFF;
  435.         however, the environment and any additional memory blocks
  436.         allocated by the program will still be swapped out.  default = 0
  437.         Note: in addition to the PSP memory block, a small second block
  438.         is used for the stack needed by the swapping code (see below).
  439.        unsigned __spawn_res_stack ;
  440.         Specify the minimum number of paragraphs of the stack to keep
  441.         resident while in the child program.  Default = 4, minimum = 2
  442.         (two paragraphs is only sufficient for bare DOS; TSRs which
  443.         hook INT 21h will increase the stack requirements, but four is
  444.         sufficient for most systems).  SPAWNO attempts to compute the
  445.         necessary stack size, but this variable allows the computed
  446.         value to be overridden if necessary.
  447.  
  448. External functions called by SPAWNO:
  449.     malloc()   \ only when passing an array of environment strings
  450.     free()       /
  451.  
  452. External variables referenced by SPAWNO:
  453.     int errno ;
  454.     int _doserrno ;
  455.     unsigned int _psp ;
  456.     char **environ ;  (only when passing an array of environment strings)
  457.  
  458. Other:
  459.     This version of SPAWNO overwrites the PSP, so the commandline
  460.     will be lost unless it was copied before the first spawn().
  461.  
  462.  
  463. -----------------
  464. Functions (Turbo Pascal):
  465.     init_SPAWNO(swap_dirs : string ; swap_types : integer ;
  466.             min_resident : integer ; resident_stack : integer) ;
  467.     spawn(program_name : string ; arguments : string ;
  468.           envseg : integer) : integer ;
  469.  
  470. Global Variables (Turbo Pascal):
  471.     spawno_error : integer ;   (error code when spawn() returns -1)
  472.  
  473.  
  474. -----------------
  475. Send comments, bug reports, etc. to
  476.  
  477.      Internet: ralf+@cs.cmu.edu
  478.      Fidonet:  Ralf Brown 1:129/26.1 (or post a message to me on DR_DEBUG--I
  479.                      have to route netmail outside Zone 1
  480.                      via Internet -> Fidonet gateways, which
  481.                      is not always reliable)
  482. or
  483.      Ralf Brown            (this address will be valid until
  484.      813 Copeland Way, Suite 26     at least November 1, 1993)
  485.      Pittsburgh, PA 15232
  486.  
  487.